//导入数据库操作模块
const db = require('../db/index')
//列出菜品
exports.fe = (req, res) => {
// 使用 SQL 查询语句查询出全部数据
const sql = `SELECT * FROM goods LIMIT 10 OFFSET 0`;    
// 执行查询并返回结果给前端
db.query(sql, (err, results) => {
  if (err) throw err;
  res.json(results);
  });
}
//列出留言
exports.messagefe = (req, res) => {
  // 使用 SQL 查询语句查询出全部数据
  const sql = `SELECT * FROM liuyan LIMIT 10 OFFSET 0`;    
  // 执行查询并返回结果给前端
  db.query(sql, (err, results) => {
    if (err) throw err;
    res.json(results);
    });
  }
//列出食材
exports.foodsfe = (req, res) => {
  // 使用 SQL 查询语句查询出全部数据
  const sql = `SELECT * FROM foods LIMIT 10 OFFSET 0`;    
  // 执行查询并返回结果给前端
  db.query(sql, (err, results) => {
    if (err) throw err;
    res.json(results);
    });
  }
  //购买食材
exports.selectFood = (req, res) => {
  // 使用 SQL 查询语句查询出全部数据
  const foods = req.query
  foods.num = "10"
  console.log(foods);
  // const sql = 'insert into foods set ?'
  const sql = 'select num from foods where name = ?'
  db.query(sql,foods.name,(err,result)=>{
     if(err) { return console.log(err.message);}
     if(result[0]) {res.json(result[0].num)}
     else { res.cc('不存在') }
    
    //  if(result) { res.json(result[0].num) }
    //  else {console.log('my');}
 })
}
exports.addFood = (req, res) => {
  // 使用 SQL 查询语句查询出全部数据
  const foods = req.query
  foods.num = "10"
  console.log(foods);
  const sql = 'insert into foods set ?'
  db.query(sql,foods,(err,result)=>{
    if(err) { return console.log(err.message);}
    if(result.affectedRows ===1 ){
      console.log('增加成功！');
  }
  res.cc('增加成功')
  });
}
exports.updateFood = (req, res) => {
  console.log(req.query);
  const foods = req.query
  const sqlStr = 'update foods set num= '+ foods.num +' where name = ?'
  db.query(sqlStr,foods.name,(err,result)=>{
     if(err) { return console.log(err.message);}
     if(result.affectedRows ===1 ){
      res.cc('更新成功')
    }
 })
}

  //删除数据
  exports.del = (req, res) => {
    const id = req.query.id
    // 使用 SQL 查询语句查询出全部数据
    const sql = `delete from goods where id=${id}`;    
    // 执行查询并返回结果给前端
    db.query(sql, (err, results) => {
      if(err) { return console.log(err.message);}
      if(results.affectedRows ===1 ){
        console.log('删除成功！');
    }
    res.cc('删除成功')
    });
    }
//更新价格
exports.updateNum = (req, res) => {
  const id = req.query.id
  const num = req.query.num
  console.log(req.query);
  const sqlStr = 'update goods set num='+num+' where id='+id+''
  db.query(sqlStr,(err,result)=>{
     if(err) { return console.log(err.message);}
     if(result.affectedRows ===1 ){
      res.cc('删除成功')
    }
 })
}
//增加菜品
exports.addGoods = (req, res) => {
  const goods = req.query
  goods.pic = 'https://img1.baidu.com/it/u=319510654,3848329935&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1686675600&t=52c28ce4526e334f1dab9d14311f1cab'
  console.log(goods);
  const sql = 'insert into goods set ?'
  db.query(sql,goods,(err,result)=>{
     if(err) { return console.log(err.message);}
     if(result.affectedRows ===1 ){
      res.cc('添加成功')
    }
 })
}
